﻿using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.DataAccess.SQLDom
{
    /// <summary>
    /// SQLServer数据库下的查询SQL语句
    /// </summary>
    /// <remarks> SQLServer数据库下的查询SQL语句</remarks>    
    [Serializable]
    internal class SelectSqlStaForMSS : SelectSqlStatement
    {
        public SelectSqlStaForMSS() : base() { }

        /// <summary>
        /// 转换成SQL语句
        /// </summary>
        /// <returns>Select SQL语句</returns>
        public override string ToSQL()
        {
            //不分页
            if (this.PageIndex <= 0 || this.PageCount <= 0)
            {
                return base.ToSQL();
            }
            //分页
            else
            { 
                StringBuilder stringBuilder = new StringBuilder();
                string mainSelectSQL = base.ToSQL();
                int startIndex = (this.PageIndex - 1) * this.PageCount;
                string select = $"{mainSelectSQL} offset {startIndex} rows fetch next {PageCount} rows only;\r\n\r\n";
                stringBuilder.Append(select);
                stringBuilder.Append(GetPagingCountSQL());
                return stringBuilder.ToString();
            }
        }
    }
}
